import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_orm.settings")
import django

django.setup()

from app01 import models

# # 基于对象的查询
author_obj = models.Author.objects.get(pk=1)
# print(author_obj.name)
# print(author_obj.books)  # 关系管理对象
# print(author_obj.books.all())  # 关系管理对象
#
book_obj = models.Book.objects.get(pk=1)
# print(book_obj.title)
# print(book_obj.authors.all())
#
# # 基于字段查询
# ret = models.Book.objects.filter(authors__name='太白')
#
# ret = models.Author.objects.filter(books__title='秃头很强')
#
# print(ret)


# all  获取所有关联的对象
# print(author_obj.books.all())
# print(book_obj.authors.all())

# set  设置多对多关系
# author_obj.books.set([3,4])   # set   [id,id,id]
# author_obj.books.set(models.Book.objects.filter(pk__in=[1,2]))   # set   [对象，对象，对象]


# add  添加多对多的关系    id   对象，
# author_obj.books.add(3,4)
# author_obj.books.add(*models.Book.objects.filter(pk__in=[3,4]))


# remove 删除多对多的关系    id   对象，
# author_obj.books.remove(3,4)
# author_obj.books.remove(*models.Book.objects.filter(pk__in=[3,4]))

# clear 清空多对多的关系
# author_obj.books.clear()

# create  新增一个所关联的对象 并且建立多对多的关系
# author_obj.books.create(title='红浪漫历险记',pub_id=1)

# book_obj.authors.create(name='alex')



